<?php
/**
 * @package   auto_occasion
 * @subpackage 
 * @author    CMS
 * @copyright 2011
 * @license    All rights reserved
 */


/**
 * Classe permettant de gerer les pubs
 *
 */
class pubSrv {

    /**
     * Fonction permettant d'avoir la liste des pubs
     * @return object $liste
     *
     */
    public function liste () {

        // instanciation de la factory
        $maFactory = jDao::get("commun~ao_pub");
        $records   = $maFactory->getListWithLocation();

        return $records->fetchAll();
    }
    /**
     * Fonction permettant d'avoir la liste des emplacemnts d'un pub
     * @return object $liste
     *
     */
    public function listeEplacement () {

        // instanciation de la factory
        $maFactory = jDao::get("commun~ao_pub_location");
        $records   = $maFactory->findAll();

        return $records->fetchAll();
    }

    /**
     * Fonction permettant d'ajouter un pub
     * @return object $pub
     *
     */
    public function ajouter ($pub) {
        // instanciation de la factory
        $maFactory = jDao::get("commun~ao_pub");
        $records   = jDao::createRecord("commun~ao_pub");

        $records->pub_name          = $pub['pub_name'];
        $records->pub_location_id   = $pub['emplacement'];
        $records->pub_tag           = $pub['content'];
        $records->pub_status        = $pub['statut'];
        $records->rubrique          = $pub['rubrique'];
      

        return $maFactory->insert($records);

    }
    
   
    /**
     * Fonction permettant de modifier un pub
     * @return object $pub
     *
     */
    public function modifier ($pub) {

        // instanciation de la factory
        $maFactory = jDao::get("commun~ao_pub");
        $records   = $maFactory->get($pub['id']);

        $records->pub_name          = $pub['pub_name'];
        $records->pub_location_id   = $pub['emplacement'];
        $records->pub_tag           = $pub['content'];
        $records->pub_status        = $pub['statut'];
        $records->rubrique          = $pub['rubrique'];


        return $maFactory->update($records);

    }

    /**
     * Récupère un pub par rubrique et position
     * @return object $pub
     *
     */
    public function getByRubriqueAndLocation ($rubrique, $location) {

        $maFactory  = jDao::get('commun~ao_pub');
        $conditions = jDao::createConditions();

        $conditions->addCondition('pub_location_id','=',$location);
        $conditions->addCondition('rubrique','=',$rubrique);
        $conditions->addCondition('pub_status','=',1);

        return $maFactory->findBy($conditions)->fetch();

    }

    /**
     * Fonction permettant d'avoir un pub à partir de son id
     * @param $id
     * @return object
     *
     */
    public function chargeParId ($id) {
        $maFactory = jDao::get("commun~ao_pub");

        $records = $maFactory->getListWithLocationById($id);

        $result = $records->fetchAll();
        return $result[0];
    }

    /**
     * Fonction permettant de supprimer un pub
     * @param $id
     * @return object
     *
     */
    public function supprimer ($id) {
        $maFactory = jDao::get("commun~ao_pub");

        return  $maFactory->delete($id);
    }

    /**
     * Fonction permettant d'initialiser un objet pu
     * @return object
     *
     */
    public function init () {

        $pub =  jDao::createRecord("commun~ao_pub");
        $pub->location_name = "";

        return $pub;
    }

	/**
	 * getQueryInsertPubMedia($_tPub)
	 *
	 *  @param array $_tPub
	 * @return string
	 */
   function getQueryInsertPub($_tPub){
  
		 $zQuery  = " INSERT INTO ao_pub   ";
		 $zQuery .= " SET  pub_name    = '" . $_tPub[pub_name]  . "',";
		 $zQuery .= "      pub_location_id    = '" . $_tPub[emplacement]  . "',";
		 $zQuery .= "      pub_tag      = '"  .$_tPub[content].  "',";
		 $zQuery .= "      pub_status     = '"  . $_tPub[statut]  .  "'";

       
		 $cnx = jDb::getConnection();
		 $cnx->exec($zQuery);
	}

}